package com.jzg.mvc.biz.cab.storeLog;

import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.Cab;
import com.jzg.common.model.biz.CabStoreLog;
import com.jzg.common.util.UuidUtil;

@Path(value = "/biz/cab/storeLog", viewPath = "/views/biz/cab/storeLog")
public class CabStoreLogController extends BaseController {

	protected final Log LOG = Log.getLog(getClass());

	public void index() {
		render("list.html");
	}

	public void listData() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String keyword = getPara("keyword");
		String sqlExceptSelect = " FROM " + CabStoreLog.TABLE_NAME + " O,"+Cab.TABLE_NAME+" C  WHERE O.CABID=C.ID ";
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " AND (O.CASENO='"+keyword+"' OR C.ORGID IN (SELECT ID FROM SYS_ORG WHERE NAME LIKE '%"+keyword+"%' ) ) ";
		}
		sqlExceptSelect +=" ORDER BY O.CREATETIME DESC ";
		Page<CabStoreLog> page = CabStoreLog.dao.paginate(pageNumber, pageSize, "select O.*,(SELECT NAME FROM SYS_ORG WHERE ID=C.ORGID) AS orgName ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}

	/***
	 * 保存
	 */
	public void save() {
		CabStoreLog cabModule = getModel(CabStoreLog.class);
		if (StrKit.notBlank(cabModule.getId())) {
			if (cabModule.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			cabModule.setId(UuidUtil.getUUID());
			if (cabModule.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}

	}

	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() throws Exception {
		String id = getPara("id");
		CabStoreLog cabStoreLog = CabStoreLog.dao.findById(id);
		if (cabStoreLog != null) {
			if (cabStoreLog.delete()) {
				renderSuccess("删除成功！");
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
}
